﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace ScarfMaker
{
    /// <summary>
    /// MaterialView.xaml 的交互逻辑
    /// </summary>
    public partial class MaterialView : Window
    {
        int nowpage = 1;
        int total = 0;
        FileInfo[] nowImages = null;
        public MaterialView()
        {
            InitializeComponent();
        }

        void rb_Checked(object sender, RoutedEventArgs e)
        {
            nowImages = (new DirectoryInfo(((RadioButton)sender).Tag as string)).GetFiles();
            ShowImages(1);
        }
        void ShowImages(int page)
        {
            if(page<1)
            {
                return;
            }
            if((page-1)*9>=nowImages.Length)
            {
                return;
            }
            
            for(int i=0;i<9;i++)
            {
                if (i < nowImages.Length - page * 9 )
                {
                    ((Image)gridImage.Children[i]).Source = new BitmapImage(new Uri("file://" + nowImages[i + page * 9].FullName));
                }
                else
                {
                    ((Image)gridImage.Children[i]).Source = null;
                }
            }
            nowpage = page;
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
                MsgControl mc = new MsgControl(((Image)sender).Source);
                mc.ShowDialog();
        }

        private void btnPrePage_Click(object sender, RoutedEventArgs e)
        {
            ShowImages(nowpage -1);
        }

        private void btnNextPage_Click(object sender, RoutedEventArgs e)
        {
            ShowImages(nowpage + 1);
        }

        private void btnBackMenu_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btnClose_Click(object sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown(0);
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            DirectoryInfo di = Models.StaticClass.diScarfData.GetDirectories("浏览").FirstOrDefault();
            if (di == null)
            {
                MessageBox.Show("未找到路径" + Models.StaticClass.diScarfData + "\\浏览");
                this.DialogResult = false;
                this.Close();
            }
            else
            {
                spMenu.Children.Clear();
                foreach (DirectoryInfo diType in di.GetDirectories())
                {
                    RadioButton rb = new RadioButton();
                    rb.Tag = diType.FullName;
                    rb.Content = diType.Name;
                    rb.Checked += rb_Checked;
                    spMenu.Children.Add(rb);
                }
                if (spMenu.Children.Count > 0)
                {
                    ((RadioButton)spMenu.Children[0]).IsChecked = true;
                }
            }
        }
    }
}
